/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "punctual_light.h"

#include "common/random_generator.h"
#include "math/ray.h"
#include "math/vector.h"

namespace Sirat {

namespace Light {

Punctual_Light::Punctual_Light(Material::Light_Material* const _light_material,const Math::Vector& _point) : Light(_light_material) , point(_point) 
{ } 

Punctual_Light::Punctual_Light() : point(Math::Vector()) { } 

Punctual_Light::Punctual_Light(const Punctual_Light& _punctual_light) : Light(_punctual_light) {
	point = _punctual_light.point;
}

Punctual_Light::~Punctual_Light() { }

Math::Vector Punctual_Light::get_point() const {
	return point;
}

void Punctual_Light::set_point(const Math::Vector& _point) {
	point = _point;
}

tuple<Math::Vector*,unsigned int> Punctual_Light::get_sample_points() const { 
	Math::Vector* points = new Math::Vector[1];
	points[0] = point;
	return make_tuple(points,1);
}
tuple<Math::Vector*,unsigned int> Punctual_Light::get_sample_points(const Common::Random_Generator& _random_generator,const unsigned int _n) const {
	return get_sample_points();
}
Math::Ray* Punctual_Light::get_sample_directions(const Common::Random_Generator& _random_generator,const unsigned int _n) const {
	Math::Ray* directions = new Math::Ray[_n];
	
	for(unsigned int i = 0; i < _n; i++) {
		Math::Vector direction;
		do {
			direction = Math::Vector(_random_generator.generate(),_random_generator.generate(),_random_generator.generate());
		} while(direction.length() > 1.0);
			direction = direction.normalize();
			Math::Ray ray(point,direction);
			directions[i] = ray;
	}
	return directions;	
}

Punctual_Light& Punctual_Light::operator=(const Punctual_Light& _punctual_light) {
	Light::operator=(_punctual_light);
	point = _punctual_light.point;
	return *this;	
}

Light* Punctual_Light::clone() {
	return new Punctual_Light(*this);
}

}

}
